package com.bawei.club.test.touch;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.ScrollView;

/**
 * @author yaotianxue
 * @description
 * @date 2025/6/4
 */
public class MyScrollView2 extends ScrollView {

    public MyScrollView2(Context context) {
        super(context);
    }

    public MyScrollView2(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    private int mLastX, mLastY;
    /**
     * 分发
     */
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        int x = (int) ev.getX();
        int y = (int) ev.getY();
        if (ev.getAction() == MotionEvent.ACTION_DOWN){
            Log.d("TAG", "dispatchTouchEvent: ACTION_DOWN");
            mLastX = x;
            mLastY = y;
        }else if (ev.getAction() == MotionEvent.ACTION_MOVE){
            Log.d("TAG", "dispatchTouchEvent: ACTION_MOVE");
            //判断方向
            int deltaX = x - mLastX;
            int deltaY = y - mLastY;
            if (Math.abs(deltaX) < Math.abs(deltaY)) { //竖滑动
                getParent().requestDisallowInterceptTouchEvent(true); //禁止父容器拦截事件 true：不拦截 false：拦截
            }
        }else if(ev.getAction() == MotionEvent.ACTION_UP){
            Log.d("TAG", "dispatchTouchEvent: ACTION_UP");
        }
        return super.dispatchTouchEvent(ev);
    }
}
